#ifndef SQLCONNECTION_H
#define SQLCONNECTION_H

#include <QSqlDatabase>
#include <QSqlQuery>
#include "console.h"
#include <vector>

struct ClientData
{
    quint32 uid;
    QString loginName;
    QString passwdHash;
    QString email;
    QString nickName;
};

struct ClientFriendrequest
{
    quint32 applicant_uid;
    QString applicant_loginName;
    QString applicant_nickName;
    QString rMessage;
};

struct ClientFriend
{
    unsigned int uid;
    QString loginName;
    QString nickName;
    bool isOnline;
    bool blocked;
};

struct ClientOfflineMessage
{
    QString friend_name;
    ulong timestamp;
    QString message;
};

class Console;
class ClientManager;
class SQLConnection
{
public:
    SQLConnection(ClientManager *clientManager, const QString &dbName, const QString &dbUser, const QString &dbPasswd, const QString &dbHost, int dbPort = 3306);
    ~SQLConnection();

    // Register && Login Methodes
    bool isRegistered(const QString &loginName);
    bool isRegistered(const quint32 &uid);
    quint32 getClientUid(const QString &loginName);
    bool checkLogin(const QString &loginName, const QString &passwdHash);

    // ClientData Methodes
    void fillClientData(ClientData *clientData, const QString &loginName);
    void fillClientData(ClientData *clientData, const quint32 &uid);

    // Change Methodes
    void changeClientNickname(quint32 &uid, const QString &newNickName);

    // Update Methodes
    void updateLastip(const quint32 &uid, const QString &ip);
    void updateLastseen(const quint32 &uid, const QString &timestamp);

    // Client Methodes
    void getFriendlist(std::vector<ClientFriend> *friendList, const QString &loginName);
    void getFriendlist(std::vector<ClientFriend> *friendList, const quint32 &uid);
    bool isFriendWith(const quint32 &uid, const quint32 &friend_uid);
    bool hasFriendrequest(const quint32 &uid, const quint32 &applicant_uid);
    void getFriendrequests(std::vector<ClientFriendrequest> *friendrequests, const QString &loginName);
    void getFriendrequests(std::vector<ClientFriendrequest> *friendrequests, const quint32 &uid);
    QString getNickname(quint32 &uid);

    // Social Methodes
    bool addFriend(const quint32 &uid, const QString &friend_loginName, const QString &rMessage);
    void acceptFriendrequest(const quint32 &uid, const quint32 &applicant_uid);
    void saveOfflineMessage(const quint32 &uid, const quint32 &friend_uid, const QString &message);
    int getOfflineMessages(std::vector<ClientOfflineMessage> *offlineMessages, const quint32 &uid);

private:
    QSqlDatabase m_database;
    Console m_console;
    ClientManager *m_clientManager;
};

#endif // SQLCONNECTION_H
